package com.zoro.dyformservice.business.user.service;

import com.zoro.dyformservice.business.user.entity.UserInfoEntity;
import com.zoro.dyformservice.business.user.repository.UserRepository;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInfoQueryService {

    private final UserRepository userRepository;

    public UserInfoQueryService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public List<UserInfoEntity> query(UserInfoEntity info) {
        Example<UserInfoEntity> userInfoEntityExample = Example.of(info);
        return this.userRepository.findAll(userInfoEntityExample);
    }

    public List<UserInfoEntity> queryExample(UserInfoEntity info) {
        ExampleMatcher matcher = ExampleMatcher.matching() // and 连接条件
                //        ExampleMatcher matcher = ExampleMatcher.matchingAny() // or 连接条件
                .withIgnorePaths("userId", "userInfoExt.id")  // 忽略 UserInfoEntity#userId  UserInfoEntity#userInfoExt#id  属性
                .withMatcher("userName", m -> m.contains().ignoreCase())
                .withIgnoreNullValues()
        ;
        return this.userRepository.findAll(Example.of(info, matcher));
    }
}
